<?php

namespace app\admin\controller\device;

use think\facade\Db;
use app\common\controller\Backend;

class Lamp extends Backend
{

    protected $noNeedLogin      = ['index','add','delete','edit','allList'];//生成土壤墒情测试数据

    public function initialize()
    {
        parent::initialize();
        //$this->model = new WeacherDeviceDataModel();
    }

    /**
     * 所有灯杆
     */
    public function allList()
    {
        $lampList = Db::name('park_lamp')->where('is_delete',0)->select();
        $this->success('获取成功',$lampList);
    }

    /**
     * 灯杆列表
     */
    public function index()
    {
        $page = $this->request->param('page');
        $status = $this->request->param('status','');
        $name = $this->request->param('name','');

        $where = [];
        $where[] = ['is_delete','=',0];
        if (!empty($status)) {
            $where[] = ['status','=',$status];
        }
        if (!empty($name)) {
            $where[] = ['name','like','%'.$name.'%'];
        }

        $lampList = Db::name('park_lamp')->where($where)->page($page,10)->select();
        $count = Db::name('park_lamp')->where($where)->count();
        $this->success('请求成功',['list'=>$lampList,'count'=>$count]);
    }

    /**
     * 添加灯杆
     */
    public function add()
    {
        $name = $this->request->post('name');
        $longitude = $this->request->post('longitude','');
        $latitude = $this->request->post('latitude','');
        $status = $this->request->post('status','');
        if (empty($name) || empty($longitude) || empty($latitude) || empty($status)) {
            $this->error('缺少参数');
        }
        Db::name('park_lamp')->insert([
            'name'=>$name,
            'longitude'=>$longitude,
            'latitude'=>$latitude,
            'status'=>$status,
            'createtime'=>date('Y-m-d H:i:s')
        ]);
        $this->success('添加成功');
    }

    /**
     * 删除灯杆
     */
    public function delete()
    {
        $id = $this->request->post('id');
        Db::name('park_lamp')->where('id',$id)->update(['is_delete'=>1]);
        $this->success('删除成功');
    }

    /**
     * 编辑灯杆
     */
    public function edit()
    {
        $id = $this->request->param('id');
        if ($this->request->isPost()) {
            $name = $this->request->post('name');
            $longitude = $this->request->post('longitude', '');
            $latitude = $this->request->post('latitude', '');
            $status = $this->request->post('status', '');
            if (empty($name) || empty($longitude) || empty($latitude) || empty($status)) {
                $this->error('缺少参数');
            }
            Db::name('park_lamp')->where('id', $id)->update([
                'name' => $name,
                'longitude' => $longitude,
                'latitude' => $latitude,
                'status' => $status,
                'updatetime' => date('Y-m-d H:i:s'),
            ]);
            $this->success('编辑成功');
        } else {
            $lamp = Db::name('park_lamp')->where('id', $id)->find();
            $this->success('请求成功',$lamp);

        }
    }
}